<?php
include_once '../tools/DBtools.php';

$accountCode = $_POST['AccountCode'];
$accountPhone = $_POST['AccountPhone'];
$realName = $_POST['RealName'];
$CardPwd = $_POST['password'];


if (empty($accountCode) || empty($accountPhone) || empty($realName) || empty($CardPwd)){
    echo '请完善开卡信息! <a href="openCard.php">返回开卡页面</a>';
}

//连接数据库
DBtools::InitDB();
$sql = "select * from accountinfo where AccountCode = '$accountCode'";
$bb = DBtools::select($sql);
if (!empty($bb)){
    echo '该账户已存在！<a href="openCard.php">返回开卡页面</a>';
    die;
}
//添加记录
$sql = "insert into accountinfo(AccountCode,AccountPhone,RealName,OpenTime)
values('$accountCode','$accountPhone','$realName',NOW())";
$gg = DBtools::noselect($sql);
if (!$gg['status']){
    echo '开户失败！<a href="openCard.php">返回开卡页面</a>';
}

$sql = "select * from accountinfo where AccountCode = '$accountCode' and RealName = '$realName'";
$msg = DBtools::select($sql);
$accountId = $msg[0]['AccountId'];
$CardNo = '62';
for ($i=0;$i<11;$i++){
    $rand = rand(0,9);
    //.=  累积
    $CardNo.= $rand;
}
//添加记录
$sql = "insert into bankcard(CardNo,AccountId,CardPwd,CardMoney,CardState,CardTime)
values('$CardNo','$accountId','$CardPwd',100,0,NOW())";
$result = DBtools::noselect($sql);

if ($result['status']){
    echo '开卡成功！<a href="../index.php">返回大厅</a>';
}else{
    echo '开卡失败！<a href="openCard.php">返回开卡页面</a>';
}
//关闭连接
DBtools::close();
